<!--
 * @Description: 
 * @Author: qingyang
 * @Date: 2022-01-11 19:05:29
 * @LastEditors: qingyang
 * @LastEditTime: 2022-12-07 18:18:01
-->
<template>
  <el-config-provider :locale="locale" size="default">
    <GAgreement></GAgreement>
    <router-view></router-view>
  </el-config-provider>
</template>

<script lang="ts">
export default {
  name: 'App'
}
</script>
<script lang="ts" setup>
import { computed, onMounted } from 'vue'
import { store } from '@/store'
import { UserActionTypes } from '@/store/modules/user/action-types'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import elementEn from 'element-plus/lib/locale/lang/en'
import { useMessage } from './hooks/useMessage'
import GAgreement from '@/businessComponents/GAgreement/index.vue'
import { AppMutationTypes } from '@/store/modules/app/mutation-types'

const { createMessage } = useMessage()
// 校验登录状态（纯前端同步校验）
store.dispatch(UserActionTypes.ACTION_CHECK_LOGIN_STATUS)
store.commit(AppMutationTypes.SET_IS_SUPPLIER, false)

const locale = computed(() => {
  let language = store.state.app.language
  return language == 'en' ? elementEn : zhCn
})

onMounted(() => {
  // 判断系统平台
  const isWin = window.navigator.userAgent.indexOf('Windows') !== -1
  if (isWin === true) {
    document.getElementById('app')?.setAttribute('class', 'is-win')
  }
  // 当窗口切换时，判断用户是否已经变化
  document.addEventListener('visibilitychange', () => {
    const localId = localStorage.getItem('id')
    const storeId = store.state.user?.userInfo?.userId
    const localEnterpriseNo = localStorage.getItem('enterpriseNo')
    const storeEnterpriseNo = store.state.user?.userInfo?.enterpriseNo
    if (!document.hidden) {
      // localStorage和vuex用户不一致，强制刷新
      if (localId && storeId && Number(storeId) !== Number(localId)) {
        createMessage.error('当前账户已失效，将会在2秒后自动刷新！')
        setTimeout(() => {
          window.location.reload()
        }, 2000)
      }
      // localStorage和vuex企业不一致，强制刷新
      if (localEnterpriseNo && storeEnterpriseNo && localEnterpriseNo !== storeEnterpriseNo) {
        window.location.reload()
      }
    }
  })
})
</script>
